package com.ruoyi.web.controller.personnel;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.entity.SafetyRulesRegulations;
import com.ruoyi.system.entity.SafetyRulesRegulationsFile;
import com.ruoyi.system.service.SafetyRulesRegulationsFileService;
import com.ruoyi.system.service.SafetyRulesRegulationsService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 安全规章制度表(SafetyRulesRegulations)表控制层
 * @author makejava
 * @since 2023-03-20 16:13:07
 */
@RestController
@RequestMapping("/safetyRulesRegulations")
public class SafetyRulesRegulationsController extends BaseController {
    @Resource
    private SafetyRulesRegulationsService safetyRulesRegulationsService;
    @Resource
    private SafetyRulesRegulationsFileService safetyRulesRegulationsFileService;

    /**
     * 分页查询
     * @param safetyRulesRegulations 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    public TableDataInfo queryByPage(SafetyRulesRegulations safetyRulesRegulations) {
        startPage();
        List<SafetyRulesRegulations> safetyRulesRegulationsList = this.safetyRulesRegulationsService.queryByPage(safetyRulesRegulations);
        if (!CollectionUtils.isEmpty(safetyRulesRegulationsList)) {
            for (SafetyRulesRegulations promise : safetyRulesRegulationsList) {
                List<SafetyRulesRegulationsFile> arrayList = new ArrayList<>();
                String fileId = promise.getFileId();
                if (StringUtils.isNotEmpty(fileId)) {
                    fileId = fileId.substring(0, fileId.length() - 1);
                    if (fileId.contains(",")) {
                        String[] split = fileId.split(",");
                        List<String> id = Arrays.asList(split);
                        if (StringUtils.isNotEmpty(id)){
                            for (String s : id) {
                                SafetyRulesRegulationsFile safetyRulesRegulationsFile = safetyRulesRegulationsFileService.queryById(Integer.valueOf(s));
                                if (!ObjectUtils.isEmpty(safetyRulesRegulationsFile)){
                                    arrayList.add(safetyRulesRegulationsFile);
                                }
                            }
                        }
                    } else {
                        SafetyRulesRegulationsFile safetyRulesRegulationsFile = safetyRulesRegulationsFileService.queryById(Integer.valueOf(fileId));
                        if (!ObjectUtils.isEmpty(safetyRulesRegulationsFile)){
                            arrayList.add(safetyRulesRegulationsFile);
                        }
                    }
                }
                promise.setRulesRegulationsFileList(arrayList);
            }
        }
        return getDataTable(safetyRulesRegulationsList);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        return AjaxResult.success(this.safetyRulesRegulationsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param safetyRulesRegulations 实体
     * @return 新增结果
     */
    @PostMapping("/create")
    public AjaxResult add(@RequestBody SafetyRulesRegulations safetyRulesRegulations) {
        return AjaxResult.success(this.safetyRulesRegulationsService.insert(safetyRulesRegulations));
    }

    /**
     * 编辑数据
     *
     * @param safetyRulesRegulations 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody SafetyRulesRegulations safetyRulesRegulations) {
        return AjaxResult.success(this.safetyRulesRegulationsService.update(safetyRulesRegulations));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @GetMapping("/delete")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.safetyRulesRegulationsService.deleteById(id));
    }

    /**
     * 导入
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "安全规章编辑", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SafetyRulesRegulations> util = new ExcelUtil<>(SafetyRulesRegulations.class);
        List<SafetyRulesRegulations> safetyRulesRegulations = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = safetyRulesRegulationsService.importExcel(safetyRulesRegulations, updateSupport, operName);
        return success(message);
    }

    /**
     * 导出
     * @param response
     */
    @Log(title = "安全规章编辑", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SafetyRulesRegulations safetyRulesRegulations) {
        List<SafetyRulesRegulations> list = safetyRulesRegulationsService.selectList(safetyRulesRegulations);
        ExcelUtil<SafetyRulesRegulations> util = new ExcelUtil<>(SafetyRulesRegulations.class);
        util.exportExcel(response, list, "安全规章编辑");
    }

    /**
     * 导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SafetyRulesRegulations> util = new ExcelUtil<>(SafetyRulesRegulations.class);
        util.importTemplateExcel(response, "安全规章编辑");
    }

}

